﻿using System;
using System.Collections.Generic;
using System.IO;
using Common.Web.Interfaces.Validator;
using ShareMarking.Service;
using TestApi.Api;
using Top.Api;
using Top.Api.Util;
using Newtonsoft.Json;
using ShareMarking.Entity;
using ComLib;

namespace ShareMarking.Test
{
	public partial class APITest : System.Web.UI.Page
    {
        public const string APPKEY = "AndriodCientAppKey";
        public const string AppSecret = "AndriodCientSecret";

	    public string ServerUrl
	    {
		    get
		    {
			    if (Request.Url.Host.Contains("localhost"))
					return "http://localhost:810/api.ashx";
				else
                    return "http://120.25.151.55:8485/api.ashx";
		    }
	    }

        protected void Page_Load(object sender, EventArgs e)
        {
	        if (!IsPostBack)
	        {
	        }
        }

        public class ItemResponse : TopResponse { }

        protected void btn_test(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("userId", "49");
            parameters.Add("nickname", "小老虎");
            parameters.Add("birthday", "2013-08-20");
            parameters.Add("duedate", "2017-08-20");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.UpdateUserInfo", parameters).Body);
            Response.End();
        }

        protected void btn_testwithfile(object sender, EventArgs e)
        {
            List<FileItem> fileList = new List<FileItem>();
            fileList.Add(new FileItem("1.mp3", bbb()));

            var parameters = new TopDictionary();
            parameters.Add("userId", "1");
            parameters.Add("duration", "1");
            parameters.Add("averageFetalHeart", "1");
            parameters.Add("recordText", "11");
            parameters.Add("title", "11");
            parameters.Add("content", "11");
            parameters.Add("circleid", 1);
            parameters.Add("share", true);
            //如果有文件上传 添加文件参数
            IDictionary<string, FileItem> fileparameters = new Dictionary<string, FileItem>();
            //把文件base64 所有文件base64传输 无法直接base64 多次转换 服务器通过mimetype转换 
            for (int i = 0; i < fileList.Count; i++)
            {
                fileparameters.Add("file_" + i.ToString(), fileList[0]);
            }

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("fetalheartApi.AddHrRecord", parameters, true, fileparameters).Body);
            Response.End();
        }

        byte[] aaa()
        {
            System.IO.MemoryStream m = new System.IO.MemoryStream();
            System.Drawing.Bitmap bp = new System.Drawing.Bitmap(UrlHelper.BaseUrl + "/image/20152728052710.png");
            bp.Save(m, System.Drawing.Imaging.ImageFormat.Gif);
            byte[] b = m.GetBuffer();
            return b;
        }

		byte[] bbb()
		{
			// 打开文件 
            FileStream fileStream = new FileStream(@"C:\Users\Public\Music\Sample Music\Kalimba.mp3", FileMode.Open, FileAccess.Read, FileShare.Read);
			// 读取文件的 byte[] 
			byte[] bytes = new byte[fileStream.Length];
			fileStream.Read(bytes, 0, bytes.Length);
			fileStream.Close();
			// 把 byte[] 转换成 Stream 
			return bytes;
		}
    
        void Do(TopDictionary parameters, string method)
        {
            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.ContentType = "text/plain";
            Response.Write(client.DoExecute<APITest.ItemResponse>(method, parameters).Body);
            Response.End();
        }

        protected void 登陆_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("mobileno", "13000000000");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.Login", parameters).Body);
            Response.End();
        }

        protected void 获取个人主要信息_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("userid", "96");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.GetUserInfo", parameters).Body);
            Response.End();
        }

        protected void 获取首页信息_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("userid", "96");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.GetMainInfo", parameters).Body);
            Response.End();
        }

        protected void 修改用户信息_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("userid", "96");
            parameters.Add("nickname", "胎心仪");
            parameters.Add("birthday", "2005-01-11");
            parameters.Add("duedate", "2015-08-11");
            parameters.Add("babyno", "1");
            parameters.Add("mobileno", "13000000000");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.UpdateUserInfo", parameters).Body);
            Response.End();
        }

        protected void 修改头像_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("userid", "96");

            List<FileItem> fileList = new List<FileItem>();
            fileList.Add(new FileItem("1.mp3", aaa()));

            //如果有文件上传 添加文件参数
            IDictionary<string, FileItem> fileparameters = new Dictionary<string, FileItem>();
            //把文件base64 所有文件base64传输 无法直接base64 多次转换 服务器通过mimetype转换 
            for (int i = 0; i < fileList.Count; i++)
            {
                fileparameters.Add("file_" + i.ToString(), fileList[0]);
            }

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.UpdateAvatar", parameters, true, fileparameters).Body);
            Response.End();
        }

        protected void 获取孕周的描述胎儿图预估体重_Click(object sender, EventArgs e)
        {

        }

        protected void 注册_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("nickname", "胎心仪");
            parameters.Add("birthday", "2005-01-11");
            parameters.Add("duedate", "2015-08-11");
            parameters.Add("babyno", "1");
            parameters.Add("mobileno", "13000000000");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("UserApi.Register", parameters).Body);
            Response.End();
        }

        protected void 添加纪念日_Click(object sender, EventArgs e)
        {

        }

        protected void 根据类型获取随记_Click(object sender, EventArgs e)
        {

        }

        protected void 获取随记详情_Click(object sender, EventArgs e)
        {

        }

        protected void 获取随记列表_Click(object sender, EventArgs e)
        {

        }

        protected void 删除随记_Click(object sender, EventArgs e)
        {

        }

        protected void 添加胎心记录_Click(object sender, EventArgs e)
        {

        }

        protected void 删除胎心记录_Click(object sender, EventArgs e)
        {

        }

        protected void 获取胎心记录列表_Click(object sender, EventArgs e)
        {

        }

        protected void 获取胎心记录详情_Click(object sender, EventArgs e)
        {

        }

        protected void 获取本周提醒_Click(object sender, EventArgs e)
        {
            var parameters = new TopDictionary();
            parameters.Add("userid", "96");
            parameters.Add("pageindex", "1");
            parameters.Add("pagesize", "10");

            //执行请求
            var client = new XiaoMaClient(ServerUrl, APPKEY, AppSecret);
            Response.Write(client.DoExecute<ItemResponse>("ArticleApi.GetCurrentWeekTips", parameters).Body);
            Response.End();
        }

        protected void 获取所有的资讯_Click(object sender, EventArgs e)
        {

        }

        protected void 根据类别获取资讯_Click(object sender, EventArgs e)
        {

        }

        protected void 获取资讯详情_Click(object sender, EventArgs e)
        {

        }

        protected void 获取类别列表_Click(object sender, EventArgs e)
        {

        }

        protected void 获取小贴士列表_Click(object sender, EventArgs e)
        {

        }

        protected void 获取小贴士详情_Click(object sender, EventArgs e)
        {

        }

        protected void 获取全部圈子列表_Click(object sender, EventArgs e)
        {

        }

        protected void 获取圈子详情_Click(object sender, EventArgs e)
        {

        }

        protected void 获取帖子列表_Click(object sender, EventArgs e)
        {

        }

        protected void 获取置顶帖子_Click(object sender, EventArgs e)
        {

        }

        protected void 发帖_Click(object sender, EventArgs e)
        {

        }

        protected void 添加评论_Click(object sender, EventArgs e)
        {

        }

        protected void 阅读记录Add1_Click(object sender, EventArgs e)
        {

        }

        protected void 得到评论列表_Click(object sender, EventArgs e)
        {

        }

        protected void 帖子点赞_Click(object sender, EventArgs e)
        {

        }
    }
}